import axios from "axios";
import router from "../../router";
import { Message, LoadingBar } from "view-design";
import qs from 'qs'
/*请求拦截*/
axios.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    const miceId = localStorage.getItem("miceId");
    if (process.env.NODE_ENV == "production") {
      config.baseURL = process.env.VUE_APP_BASEURL;
    } else {
      config.baseURL = "/api";
    }
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    if(token){
      config.headers["accessToken"] = token;
    }
    if(miceId){
      config.headers["miceId"] = miceId;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
/*请求响应拦截*/
axios.interceptors.response.use(
  (response) => {
    switch (response.data.code) {
      case -4001:
        console.log('response',response)
        Message.destroy();
        Message.info(response.data.msg);
        localStorage.clear();
        router.push("/");
        break;
      case 40010:

        break;
      default:
        return response;
    }
  },
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          Message.error("错误请求");
          break;
        case 401:
          Message.error("未授权，请重新登录");
          break;
        case 403:
          Message.error("拒绝访问");
          break;
        case 404:
          Message.error("请求错误,未找到该资源");
          break;
        case 405:
          Message.error("请求方法未允许");
          break;
        case 408:
          Message.error("请求超时");
          break;
        case 422:
          Message.error("权限已过期");
          break;
        case 500:
          Message.error("服务器端出错");
          break;
        case 501:
          Message.error("网络未实现");
          break;
        case 502:
          Message.error("网络错误");
          break;
        case 503:
          Message.error("服务不可用");
          break;
        case 504:
          Message.error("网络超时");
          break;
        case 505:
          Message.error("http版本不支持该请求");
          break;
        default:
          Message.error(`连接错误${err.response.status}`);
      }
      return new Promise(() => {});
    } else {
      return Promise.reject(error);
    }
  }
);

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, para) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: para,
      })
      .then((response) => {
        resolve(response.data);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url,
      data:qs.stringify(params)
    }).then((response) => {
      resolve(response.data);
    })
    .catch((error) => {
      reject(error);
    });
     
  });
}
export function put(url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url,
      data:params
    }).then((response) => {
      resolve(response.data);
    })
    .catch((error) => {
      reject(error);
    });
     
  });
}
